//
// process.cpp
// libprotea
// Tools Library for the Protea Project / Process/Thread functionality
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

#include "../defines.h"
#include "../AppHandler/AppInterface.h"
#include "process.h"
#include "pipes.h"
	
process_info* XCAddProcess( process_info_ptr& plist ) 
{
	process_info* retval = plist;
	if (plist) 
	{
		while (retval->next)
			retval = retval->next;
		retval->next = new process_info;
		retval = retval->next;
	} else
		plist = retval = new process_info;

	retval->next = NULL;
	retval->file = NULL;

	return retval;
};

void XCDeleteProcess( process_info_ptr& plist, pid_t pid ) 
{
	process_info* temp = plist;
	if (temp) 
	{
		if (temp->pid == pid) 
		{
			if (!temp->port) {
				plist = temp->next;
				delete temp;
			} else 
				temp->active = false;
		} 
		else 
		{
			while (temp->next && temp->next->pid != pid)
				temp = temp->next;

			if (temp->next) {
				if (!temp->port) {
					process_info* save = temp->next;
					temp->next = temp->next->next;
					delete save;
				} else
					temp->active = false;
			};
		};
	};
};

process_info* XCGetProcess( process_info_ptr plist, pid_t pid ) 
{
	process_info* retval = NULL;
	process_info* temp = plist;
	
	if (temp)
	{
		if (temp->pid == pid)
		{
			retval = temp;
		}
		else
		{
			while (temp->next && temp->next->pid != pid)
				temp = temp->next;
			if (temp->next && temp->next->pid == pid)
				retval = temp->next;
		}
	}
	return retval;
};

int XCShellExecute( char* cmd )
{
	int retval = 0;
//	PORT_HANDLE handle = XCCreateProcess( file, pcount, parms, 0 );
	return retval;
};
